//
//  OrderCellView.m
//  JiaoX
//
//  Created by yinqy on 15/1/15.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "OrderCellView.h"
#import "LeftLineView.h"
#import "OrderProdItemView.h"

@interface OrderCellView()
{
    LeftLineView *_orderCode;//订单号
    UILabel *_statusLabel;
    UIButton *_timeBtn;
    OrderProdItemView *_prodItem1;
    OrderProdItemView *_prodItem2;
    
    UIView *_splite1;
    UIView *_splite2;
    UIView *_splite3;
    
    UILabel *_allPriceLabel;
    UILabel *_allNum;
    
    UIButton *_orderEnterBtn;
    
    UIButton *_btn;
    
    
}

@end
@implementation OrderCellView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_order_secret"]];
        image.y = JXPublicInset;
        image.x = JXScreenW-image.width-27;
        [self addSubview:image];
        
        _orderCode = [[LeftLineView alloc]initWithTitleFontSize:ORDER_CODE_FONT_SIZE];
        [_orderCode.label setTextColor:[UIColor blackColor]];
        [self addSubview:_orderCode];
        
        _statusLabel = [[UILabel alloc]init];
        [_statusLabel setTextColor:[UIColor blackColor]];
        [_statusLabel setFont:[UIFont systemFontOfSize:ORDER_CODE_FONT_SIZE]];
        
        [self addSubview:_statusLabel];
        
        //时间
        _timeBtn = [[UIButton alloc]init];
        [_timeBtn.titleLabel setFont:[UIFont systemFontOfSize:ORDER_TITME_FONT_SIZE]];
        [_timeBtn setTitleColor:JXColor(102, 102, 102) forState:UIControlStateNormal];
        [_timeBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        [_timeBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [_timeBtn setImage:[UIImage imageNamed:@"ic_time"] forState:UIControlStateNormal];
        [_timeBtn setUserInteractionEnabled:false];
        [self addSubview:_timeBtn];
        
        _prodItem1 = [[OrderProdItemView alloc]init];
        [_prodItem1 setUserInteractionEnabled:YES];
        [self addSubview:_prodItem1];
        _prodItem2 = [[OrderProdItemView alloc]init];
        [_prodItem2 setUserInteractionEnabled:YES];
        [self addSubview:_prodItem2];
        
        _allPriceLabel = [[UILabel alloc]init];
        [_allPriceLabel setTextColor:JXColor(102, 102, 102)];
        [_allPriceLabel setFont:[UIFont systemFontOfSize:ORDER_CODE_FONT_SIZE]];
        [self addSubview:_allPriceLabel];
        
        _allNum = [[UILabel alloc]init];
        [_allNum setFont:[UIFont systemFontOfSize:ORDER_TITME_FONT_SIZE]];
        [_allNum setTextColor:JXColor(77, 77, 77)];
        [self addSubview:_allNum];
        
        _orderEnterBtn = [[UIButton alloc]init];
        [_orderEnterBtn.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:15]]];
        [_orderEnterBtn setTitleColor:JXThemeColor forState:UIControlStateNormal];
        [_orderEnterBtn.layer setCornerRadius:5];
        [_orderEnterBtn.layer setBorderColor:JXThemeColor.CGColor];
        [_orderEnterBtn.layer setBorderWidth:1];
        [_orderEnterBtn setSize:CGSizeMake(100, 100)];

        [self addSubview:_orderEnterBtn];
        
        _splite1 = [[UIView alloc]init];
        [_splite1 setBackgroundColor:JXColor(204, 204, 204)];
        [self addSubview:_splite1];
        
        _splite2 = [[UIView alloc]init];
        [_splite2 setBackgroundColor:JXColor(204, 204, 204)];
        [self addSubview:_splite2];
        
        _splite3 = [[UIView alloc]init];
        [_splite3 setBackgroundColor:JXColor(204, 204, 204)];
        [self addSubview:_splite3];
        
        
    }
    return self;
}



-(void)setOrderFrame:(OrderItemFrame *)orderFrame{
    _orderFrame = orderFrame;
    self.frame = orderFrame.selfFrame;
    OrderListItem *item = orderFrame.orderItem;
    _orderCode.frame = orderFrame.orderCodeFrame;
    [_orderCode setText:[NSString stringWithFormat:@"订单号：%@",item.orderCode] constrainedToSize:orderFrame.orderCodeFrame.size];
    
    _statusLabel.frame = orderFrame.orderStatusFrame;
    [_statusLabel setText:item.orderStatusStr];
    
    _timeBtn.frame = orderFrame.orderTimeFrame;
    [_timeBtn setTitle:item.orderCreTime forState:UIControlStateNormal];
    
    _splite1.frame = orderFrame.orderSplit1Frame;
    _prodItem1.frame = orderFrame.orderProd1Frame;
    [_prodItem1 setCartProdSub:item.prodList.firstObject];
    
    _prodItem2.frame = orderFrame.orderProd2Frame;
    if(item.prodList.count>1){
        [_prodItem2 setHidden:NO];
        [_prodItem2 setCartProdSub:item.prodList.lastObject];
    }else{
        [_prodItem2 setHidden:YES];
    }
    
    _splite2.frame = orderFrame.orderSplit2Frame;
    _splite3.frame = orderFrame.orderSplit3Frame;
    
    _allPriceLabel.frame = orderFrame.orderAllPriceFrame;
    
    NSMutableDictionary *rangs = [NSMutableDictionary dictionary];
    UIColor *color1 = [UIColor blackColor];
    UIColor *color2 = JXThemeColor;
    NSInteger index = 0; 
    NSMutableString *content = [[NSMutableString alloc] initWithString:@"总计："];
    //设置名称的rang
    index = index + content.length;
    [rangs setObject:color1 forKey:NSStringFromRange(NSMakeRange(0, index))];
    [content appendFormat:@"￥%@",item.orderPrice];
    [rangs setObject:color2 forKey:NSStringFromRange(NSMakeRange(index, item.orderPrice.length+1))];
    index = content.length;
    [content appendFormat:@"(运费:￥%@)",item.freight];
    [rangs setObject:[UIFont systemFontOfSize:10] forKey:NSStringFromRange(NSMakeRange(index, content.length-index))];
    [_allPriceLabel setAttributedText:[Utils getAttribtString:content attrRangs:rangs]];
    
    _allNum.frame = orderFrame.prodCountFrame;
    // 设置件数
    NSUInteger size = item.prodList.count;
    if (size > 2) {
        [_allNum setText:[NSString stringWithFormat:@"还有%lu件",(unsigned long)(size-2)]];
    } else {
        [_allNum setText:[NSString stringWithFormat:@"共%lu件",(unsigned long)size]];
    }
    
    _orderEnterBtn.frame = orderFrame.orderEnterFrame;

    if ((item.payMode == 2 | item.payMode == 3) && item.orderStatus == 1)
        [_orderEnterBtn setTitle:@"立即支付" forState:UIControlStateNormal];
    else
        [_orderEnterBtn setTitle:@"订单详情" forState:UIControlStateNormal];
    [_orderEnterBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)click:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(orderItemBtnClick:)]) {
        [_delegate orderItemBtnClick:self.tag];
    }
}


@end
